package cn.dcw.pethome.user.controller;

import cn.dcw.pethome.basic.exception.MyException;
import cn.dcw.pethome.basic.util.AjaxResult;
import cn.dcw.pethome.user.dto.LoginDto;
import cn.dcw.pethome.user.service.ILoginService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping
public class LoginController {

    @Autowired
    private ILoginService service;
    /**
     * 登入
     */
    @PostMapping("/login")
    @ApiOperation(value = "登入",notes = "后台登入")
    public AjaxResult otLogin(@RequestBody LoginDto loginDto){
        try {
            AjaxResult ajaxResult = service.otLogin(loginDto);
            return ajaxResult;
        }catch (MyException e) {//用户信息有误
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {//用户信息有误
            e.printStackTrace();
            return new AjaxResult(false,"系统错误");
        }
    }
    /**
     * 微信登入
     */
    @PostMapping("/login/wechat")
    @ApiOperation(value = "微信登入",notes = "微信登入")
    public AjaxResult wechatLogin(@RequestBody Map<String,String> map){
        try {
            AjaxResult ajaxResult = service.wechatLogin(map);
            return ajaxResult;
        }catch (MyException e) {//用户信息有误
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {//用户信息有误
            e.printStackTrace();
            return new AjaxResult(false,"系统错误");
        }
    }
    /**
     * 微信绑定
     */
    @PostMapping("/login/binder/wechat")
    @ApiOperation(value = "微信绑定",notes = "微信绑定")
    public AjaxResult wechatBinder(@RequestBody Map<String,String> map){
        try {
            Map<String, Object> resultObj = service.wechatBinder(map);
            return new AjaxResult(true,"操作成功",resultObj);
        }catch (MyException e) {//用户信息有误
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {//用户信息有误
            e.printStackTrace();
            return new AjaxResult(false,"系统错误");
        }
    }
}
